package org.example.Soution004;

import java.util.HashMap;
import java.util.Map;

public class SkillMostElement {
    public static void main(String[] args) {
        SkillMostElement obj = new SkillMostElement();
        int[] nums = new int[]{6,5,5};
        System.out.println(obj.majorityElement(nums));
    }

    public int majorityElement2(int[] nums) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int max=Integer.MIN_VALUE;int res=0;
        for(int num:nums){
            map.put(num, map.getOrDefault(num, 0) + 1);
            if(map.get(num) > max){
                max = map.get(num);
                res=num;
            }
        }
        return res;
    }

    public int majorityElement(int[] nums) {
        int res=0;int count=0;
        for(int num:nums){
            if(count==0){
                res=num;
            }
            count+= res==num?1:-1;
        }
        return res;
    }
}
